package com.jianganming.transparent;

import java.util.ArrayList;
import java.util.List;

//组合模式
public class Composite extends Component {
    List<Component> components;

    public List<Component> getComponents() {
        return components;
    }

    public Composite(String name) {
        super(name);
        this.components = new ArrayList<>();
    }

    @Override
    public String operation(String t) {
        StringBuilder sb = new StringBuilder(t+this.name);
        t+="  ";
        for (Component component : components) {
            sb.append("\n");
            sb.append(component.operation(t));
        }
        return sb.toString();
    }

    @Override
    public boolean addChild(Component component) {
        return components.add(component);
    }

    @Override
    public boolean removeChild(Component component) {
        return components.remove(component);
    }

    @Override
    public Component getChild(int index) {
        return components.get(index);
    }
}
